//引用下载包
const express=require('express');
const expressStatic=require('express-static');
const slideRouter=require('./routes/slide');
const dbConnect=require('./utils/db/mongdbConnect');
const userRouter=require('./routes/user');
const childRouter=require('./routes/child');
const heRouter=require('./routes/he');
const sheRouter=require('./routes/she');
const mobRouter=require('./routes/mob');
const viewRouter=require('./routes/views');
const bodyParser=require('body-parser');
const cookieParser=require('cookie-parser');
const cookieSession=require('cookie-session');
// const multer=require('multer');

//开启服务
var app= express();

app.listen(8080,function(err){
    if(!err){
        console.log('app is running!!');
    }else {
        console.log('app is error!');
    }
});
//开启数据连接
dbConnect.dbConnect();
//应用一下bodyParser
app.use(bodyParser.urlencoded({extend:true}));

//启用cookie
var keys=[];
for (var i=0;i<100;i++){
    var str='abcd133%%^&'+Math.random(1000000);
    keys.push(str);
}
app.use(cookieParser());
app.use(cookieSession({
    name:'shoreOnline',
    keys:keys,
    maxAge:20*60*1000
}))



//开启路由
app.use('/slide',slideRouter.router);
app.use('/user',userRouter.router);
app.use('/views',viewRouter.router);
app.use('/child',childRouter.router);
app.use('/he',heRouter.router);
app.use('/she',sheRouter.router);
app.use('/mob',mobRouter.router);




//起始页
app.use('/',function (req,res,next) {
    var options={
        root:__dirname + '/www/',
        _dotfiles:'deny',
        headers:{
            'x-timestamp':Date.now(),
            'x-sent':true
        }
    };
    var url=req.url;
    //根文件夹与非根的操作
    if (url.indexOf('/')==0&&url.length>1){
        next();
    }else {
        res.sendFile('index.html', options, function (err) {
            if (err) {
                console.log('index.html is error!');
            }
        })
    }
});

//设置静态页面
app.use(expressStatic('./www'));